--英雄系统功能
--#include "data\config\hero\HeroSwingConfig.lua" once
--#include "data\functions\Activity\OpenServer\OpenServerHeroSwing.lua" once


--[[激活英雄翅膀]]
function ActivateHeroSwing( sysarg, heroSwingIdx )
	--print("ActivateHero, heroSwingIdx="..heroSwingIdx)

	local heroSwingCfg = HeroSwingConfig[heroSwingIdx]
	if not heroSwingCfg then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.HeroSwing001,ttFlyTip)
		return
	end

	local heroSwing = Actor.getIntProperty(sysarg, PROP_ACTOR_HEROSWING)
	local usingHeroSwing = Actor.getIntProperty(sysarg, PROP_ACTOR_USING_HERO_SWING)

	if System.getIntBit(heroSwing, heroSwingIdx-1) == 1 then
        Actor.sendTipmsg( sysarg, Lang.ScriptTips.HeroSwing003 ,ttFlyTip )
        return
    end

	if not CheckActivateHeroSwingCond( sysarg, heroSwingCfg ) then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.HeroSwing002,ttFlyTip)
		return
	end

	heroSwing = System.setIntBit(heroSwing, heroSwingIdx-1, true)
	--print("heroSwing ="..heroSwing)
	Actor.setIntProperty(sysarg, PROP_ACTOR_HEROSWING, heroSwing)	

	if usingHeroSwing and usingHeroSwing == 0 then
		SetHeroSwingState( sysarg, heroSwingIdx, 1 )
	end

	OpenServerHeroSwingActive(sysarg, heroSwingIdx)
end

--[[
检查激活英雄的条件
]]
function CheckActivateHeroSwingCond( sysarg, heroCfg )
	if not heroCfg then
		return false
	end
	if not heroCfg.activateCond then
		return true
	end
	if #heroCfg.activateCond == 0 then
		return true
	end
	for i, cond in ipairs( heroCfg.activateCond ) do
		if CheckActivateHeroOneCond( sysarg, cond ) then
			return true
		end
	end
	return false
end

--[[
激活英雄的条件
]]
function CheckActivateHeroOneCond( sysarg, conds )
	for i, cond in ipairs( conds ) do
		if not CheckActivateHeroOneCondImp( sysarg, cond ) then
			return false
		end
	end
	return true
end

function CheckActivateHeroOneCondImp( sysarg, cond )
	if cond.level then				--等级要求
		if Actor.checkActorLevel( sysarg, cond.level, 0 ) then
			return true
		end
		return false
	elseif cond.openDay then
		local days = System.getDaysSinceOpenServer()
		if days >= cond.openDay then
			return true
		end
		return false
	elseif cond.hasSwingIdx then
		local heroSwing = Actor.getIntProperty(sysarg, PROP_ACTOR_HEROSWING)
		if System.getIntBit(heroSwing, cond.hasSwingIdx-1) == 1 then
	        return true
	    end
		return false
	elseif cond.vipGrade then		--vip等级要求
		if Actor.getVipGrade( sysarg ) >= cond.vipGrade then
			return true
		end
		return false
	elseif cond.loginDay then		--登陆天数要求
		local loginDays = Actor.getLoginDays(sysarg)
		if loginDays >= cond.loginDay then
			return true
		end
		return false
	elseif cond.yuanBao then		--需要元宝
		if Actor.IsCross( sysarg ) then
			Actor.sendTipmsg(sysarg,Lang.ScriptTips.HeroSwing008,ttFlyTip)
			return false
		end

		local myYuanBao = Actor.getMoneyCount(sysarg, mtYuanbao)
		if myYuanBao < cond.yuanBao then
			Actor.sendItemLack( sysarg, qatYuanbao, 0, cond.yuanBao )
			return false
		end

		if not Actor.delMoney( sysarg, mtYuanbao, cond.yuanBao, LogIds.HeroSwingLogId, Lang.ScriptTips.HeroLog002 ) then
			return false
		end
		return true
	end
	return false
end

function SetHeroSwingState( sysarg, heroSwingIdx, newState )
	local heroSwing = Actor.getIntProperty(sysarg, PROP_ACTOR_HEROSWING)
	local usingHeroSwing = Actor.getIntProperty(sysarg, PROP_ACTOR_USING_HERO_SWING)
	--print("SetHeroState, heroIdx="..heroIdx..", newState="..newState)
	local heroSwingCfg = HeroSwingConfig[heroSwingIdx]
	if not heroSwingCfg then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.HeroSwing004,ttFlyTip)
		return
	end

	if System.getIntBit(heroSwing, heroSwingIdx-1) ~= 1 then
        Actor.sendTipmsg( sysarg, Lang.ScriptTips.HeroSwing005 ,ttFlyTip )
        return
    end
	
	if newState == 1 and heroSwingIdx == usingHeroSwing then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.HeroSwing006 ,ttFlyTip )
		return
	elseif newState == 0 and newState == usingHeroSwing then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.HeroSwing007 ,ttFlyTip )
		return 
	end
	--print("heroSwingIdx ="..heroSwingIdx)
	--print("newState ="..newState)
	Actor.SetHeroSwingState(sysarg, heroSwingIdx, newState)
end